﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace WindowsFormsApplication
{
    public partial class FileForm : Form
    {
        public FileForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = folderBrowserDialog1.ShowDialog();
                string path = folderBrowserDialog1.SelectedPath;
                DirectoryInfo di = new DirectoryInfo(path);
                string sourceFile = string.Empty;
                string desFile = string.Empty;
                foreach (FileInfo f in di.GetFiles())
                {
                    sourceFile = Path.Combine(path, f.Name);
                    desFile = Path.Combine(path, f.Name.Substring(3, f.Name.Length - 3));
                    File.Copy(sourceFile, desFile);
                    f.Delete();
                }
                MessageBox.Show("OK");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr1 = folderBrowserDialog1.ShowDialog();
                string path1 = folderBrowserDialog1.SelectedPath;
                DirectoryInfo di1 = new DirectoryInfo(path1);

                DialogResult dr2 = folderBrowserDialog1.ShowDialog();
                string path2 = folderBrowserDialog1.SelectedPath;
                DirectoryInfo di2 = new DirectoryInfo(path2);
                int counter = 0;
                foreach (FileInfo f1 in di1.GetFiles())
                {
                    foreach (FileInfo f2 in di2.GetFiles())
                    {
                        if (f1.Name.Contains(f2.Name) || f2.Name.Contains(f1.Name))
                        {
                            textBox1.Text += string.Format("{0}\r\n", f1.Name);
                            counter++;
                        }
                    }
                }
                textBox1.Text = string.Format("共{0}条重名的文件，详细信息如下： \r\n{1}", counter, textBox1.Text);
                MessageBox.Show("OK");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
